package com.fare.taluo.fetl.domain.repository.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.fare.taluo.fetl.domain.base.EntityFetlHibernateRepository;
import com.fare.taluo.fetl.domain.entity.FreightMontiorEntity;
import com.fare.taluo.fetl.domain.entity.FreightPriceEntity;
import com.fare.taluo.fetl.domain.repository.IFreightPriceRepository;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.util.StringAndDateAndFileUtil;

@Repository
public class FreightPriceFetlRepositoryImpl extends EntityFetlHibernateRepository implements IFreightPriceRepository{
	/**
	 * 获得比配置日期要后的价差记录
	 */
	public List<FreightPriceEntity> findByConditionSpread(String fromCity,String toCity,String grabChannelKey,String flightNo,Date flightDate,String compareGrabChannelKey,Date createTime,Integer continueDays)	{
		List<Object> params = new ArrayList<Object>();

		String hql = "from com.fare.taluo.fetl.domain.entity.FreightPriceEntity entity  where 1=1 ";

		if(StringAndDateAndFileUtil.checkString(fromCity)){
			hql += " and entity.fromCity= ?";
			params.add(fromCity);
		}
		if(StringAndDateAndFileUtil.checkString(toCity)){
			hql += " and entity.toCity= ?";
			params.add(toCity);
		}
		if(StringAndDateAndFileUtil.checkString(grabChannelKey)){
			hql += " and entity.grabChannelKey= ?";
			params.add(grabChannelKey);
		}
		if(StringAndDateAndFileUtil.checkString(flightNo)){
			hql += " and entity.flightNo= ?";
			params.add(flightNo);
		}
		if(flightDate != null){
			Calendar cal = Calendar.getInstance();
			cal.setTime(flightDate);
			cal.add(Calendar.DATE, 1);
			hql += " and entity.flightDate >= ? and  entity.flightDate <= ?";
			params.add(flightDate);
			params.add(cal.getTime());
		}
		if(flightDate != null){
			hql += " and entity.compareGrabChannelKey= ? ";
			params.add(compareGrabChannelKey);
		}
		if(createTime != null){
			String dateStr =  DateUtils.formatDay(createTime, "yyyy-MM-dd");
			Date date = DateUtils.strToDate(dateStr, "yyyy-MM-dd");
			hql += " and entity.grabDate < ?";
			params.add(date);
		}

		hql += " order by entity.grabDate  desc";
		Session session =getSessionFactory1().openSession();
		//Session session  =	super.getHibernateTemplate1().getSessionFactory().openSession();
		Query query = session.createQuery(hql);
		List<FreightPriceEntity> list = null;
		try {
			query = session.createQuery(hql);
			for (int i = 0; i < params.size(); i++) {
				query = query.setParameter(i, params.get(i));
			}
			query.setFirstResult(0);
			query.setMaxResults(continueDays);
			 list = query.list();
		} catch (Exception e) {
			e.printStackTrace();	
		}finally{
			session.close();
		}
		
		return list;
	}
	public List<FreightPriceEntity> findByConditionContrast(String fromCity,String toCity,String grabChannelKey,
															String flightNo,Date flightDate,String cabinLevel,
							String compareGrabChannelKey,String contrastFlightNo,String contrastCabinLevel,Date createTime,Integer continueDays){
		List<Object> params = new ArrayList<Object>();

		String hql = "from com.fare.taluo.fetl.domain.entity.FreightPriceEntity entity  where 1=1 ";

		if(StringAndDateAndFileUtil.checkString(fromCity)){
			hql += " and entity.fromCity= ?";
			params.add(fromCity);
		}
		if(StringAndDateAndFileUtil.checkString(toCity)){
			hql += " and entity.toCity= ?";
			params.add(toCity);
		}
		if(StringAndDateAndFileUtil.checkString(grabChannelKey)){
			hql += " and entity.grabChannelKey= ?";
			params.add(grabChannelKey);
		}
		if(StringAndDateAndFileUtil.checkString(flightNo)){
			hql += " and entity.flightNo= ?";
			params.add(flightNo);
		}
		if(flightDate != null){
			hql += " and entity.flightDate= ?";
			params.add(flightDate);
		}
		if(StringAndDateAndFileUtil.checkString(cabinLevel)){
			hql += " and entity.cabinLevel= ?";
			params.add(cabinLevel);
		}
	
		
		
		if(flightDate != null){
			hql += " and entity.compareGrabChannelKey= ?";
			params.add(compareGrabChannelKey);
		}
		
		if(StringAndDateAndFileUtil.checkString(contrastFlightNo)){
			hql += " and entity.compareFlightNo= ?";
			params.add(contrastFlightNo);
		}
		
		if(StringAndDateAndFileUtil.checkString(contrastCabinLevel)){
			hql += " and entity.compareCabinLevel= ?";
			params.add(contrastCabinLevel);
		}
		
		
		if(createTime != null){
			String dateStr =  DateUtils.formatDay(createTime, "yyyy-MM-dd");
			Date date = DateUtils.strToDate(dateStr, "yyyy-MM-dd");
			hql += " and entity.grabDate < ?";
			params.add(date);
		}

		hql += " order by entity.grabDate  desc";
		Session session =getSessionFactory1().openSession();
		//Session session  =	super.getHibernateTemplate1().getSessionFactory().openSession();
		Query query = session.createQuery(hql);
		List<FreightPriceEntity> list = null;
		try {
			query = session.createQuery(hql);
			for (int i = 0; i < params.size(); i++) {
				query = query.setParameter(i, params.get(i));
			}
			query.setFirstResult(0);
			query.setMaxResults(continueDays);
			 list = query.list();
		} catch (Exception e) {
			e.printStackTrace();	
		}finally{
			session.close();
		}
		
		return list;
	}
	
	
	
}
